const { HYEventStore } = require('hy-event-store')
const axios = require('axios')

const userStore = new HYEventStore({
	state: {
		name: 'why',
		age: 18,

		banners: [],
		recommends: [],
	},
	actions: {
		fetchHomeMultidateAction(ctx) {
			axios.get('http://123.207.32.32:8000/home/multidata').then((res) => {
				ctx.banners = res.data.data.banner.list
			})
		},
	},
})

// aaa.js中使用共享的数据
/* userStore.onState('name', (val) => {
	console.log('val: ', val)
})

userStore.onState('banners', (val) => {
	console.log('val: ', val)
}) */

userStore.onStates(['name', 'banners'], (val) => {
	console.log(val.name)
	console.log(val.banners)
})

// bbb.js改变数据
setTimeout(() => {
	userStore.setState('name', 'kobe')
	// 发送请求
	userStore.dispatch('fetchHomeMultidateAction')
}, 2000)
